package com.bjsxt.userscenter.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.util.DateFormatUtil;
import com.bjsxt.userscenter.common.util.EncryptUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.common.util.VerifyCodeUtils;

/**
 * 所有Controller的父类
 * @author WangshSxt
 *
 */
public class BaseController
{
	@Resource
	protected DateFormatUtil dateFormatUtil;
	@Resource
	protected EncryptUtil encryptUtil ;
	
	/**
	 * 页面上提示信息
	 */
	protected String info ; 
	
	/**
	 * 生成分页对象
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	protected PageInfoUtil proccedPageInfo(String currentPage, String pageSize)
	{
		/* 分页 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		try
		{
			/* 将字符串转换成整数,有风险,
			 * 字符串为a,转换不成不整数
			 *  */
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil;
	}
	
	/**
	 * 生成验证码
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/randImg")
	public void randImg(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.setContentType("image/jpeg");  
          
        //生成随机字串  
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);  
        //存入会话session  
        HttpSession session = request.getSession(true);  
        session.setAttribute("rand", verifyCode);  
        //生成图片  
        int w = 200, h = 80;  
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode); 
	}
	
	/**
	 * 拼装Jui要求的返回结果
	 * @param info
	 * @return
	 */
	protected JSONObject printJuiJson(HttpServletRequest request,String info)
	{
		String statusCode = request.getParameter("statusCode");
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String callbackType = request.getParameter("callbackType");
		String forwardUrl = request.getParameter("forwardUrl");
		String confirmMsg = request.getParameter("confirmMsg");
		
		if(navTabId == null)
		{
			navTabId = "" ; 
		}
		if(rel == null)
		{
			rel = "" ; 
		}
		if(callbackType == null)
		{
			callbackType = "" ; 
		}
		if(forwardUrl == null)
		{
			forwardUrl = "" ; 
		}
		
		JSONObject resultJSON = new JSONObject() ; 
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", info);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON;
	}
}
